package 栈;
import java.util.Stack;
public class 括号匹配 {
    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(int i = 0; i < s.length(); i++){
            if(s.charAt(i) == ('(')
                    ||s.charAt(i)==('[')
                    || s.charAt(i)==('{')){
                stack.push(s.charAt(i));
            }else{
                if(stack.isEmpty()){
                    return false;
                }
                Character pop = stack.pop();
                if (pop == '('  && s.charAt(i) != ')'){
                    return false;
                }
                if (pop == '['  && s.charAt(i) != ']'){
                    return false;
                }
                if (pop == '{'  && s.charAt(i) != '}'){
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }
    public static void main(String[] args) {
        boolean valid = isValid("()[]{}");
        System.out.println(valid);
    }
}
